import { cloneDeep } from 'lodash-es'
import type { InitData, MergeClo, MergeParams } from '@/components/common/typing/table'
const mergeCloFn = (arr:string[], item:InitData, col:string[], colLen:number) => {
  let isEqual = false
  const mergeFirstColIndex:number = arr.indexOf(col[0])
  let proClo = item[col[0]]
  for (const key of col) {
    if (proClo === item[key]) {
      proClo = item[key]
      isEqual = true
    } else {
      isEqual = false
      break
    }
  }
  return isEqual
    ? {
        mergeFirstColIndex,
        colLen
      }
    : false
}
const mergeFirstRow = (data:InitData[], oneItem:InitData, item:string) => {
  const i:number = oneItem.firstLine
  const merge = cloneDeep(data[i]?.merge)
  const rowData = {
    mergeFirstRow: i,
    rowLen: oneItem.len,
    key: item
  }
  if (oneItem.len !== 0) {
    if (merge) {
      const row = merge.row || []
      row.push(rowData)
      Object.assign(data[i].merge, {
        row
      })
    } else {
      data[i].merge = {
        row: [rowData]
      }
    }
  }
  return data
}
const getRowSpanMethod = async (data:InitData[], row:string[]) => {
  const rowMapItem:InitData = {
    firstLine: 0,
    pre: '',
    len: 0,
    isStarted: true
  }
  const dataLen = data.length
  console.log(data)
  for (const item of row) {
    let oneItem = cloneDeep(rowMapItem)
    for (const [index, items] of data.entries()) {
      if (!oneItem.isStarted) {
        const i:number = oneItem.firstLine
        if (oneItem.pre === items[item]) {
          oneItem.len = index - (oneItem.firstLine || 0)
          const rowItem = {
            hidden: true,
            key: item
          }
          if (items?.merge) {
            const row = items.merge.row || []
            row.push(rowItem)
            items.merge.row = row
          } else {
            items.merge = {
              row: [rowItem]
            }
          }
          Object.assign(data[index], items)
          // 最后一个的merge
          if (dataLen - 1 === index && i !== index) {
            mergeFirstRow(data, oneItem, item)
          }
        } else {
          data = mergeFirstRow(data, oneItem, item)
          oneItem = cloneDeep(rowMapItem)
          Object.assign(oneItem, {
            firstLine: index,
            pre: items[item],
            isStarted: false
          })
        }
      } else {
        Object.assign(oneItem, {
          firstLine: index,
          pre: items[item],
          len: 0,
          isStarted: false
        })
      }
    }
  }
  return data
}

const getColSpanMethod = (col:string[], data:InitData[]) => {
  const colLen = col.length
  const dataLen = data.length
  if (!colLen || !dataLen) return
  const arr = Object.keys(data[0])
  for (const [index, items] of data.entries()) {
    const mergeClo:MergeClo|Boolean = mergeCloFn(arr, items, col, colLen)
    // 列合并
    if (mergeClo) {
      Object.assign(items, { merge: {} })
      Object.assign(items.merge, {
        col: mergeClo
      })
    }
    data[index] = items
  }
  return data
}

export const getMergeData = async (data:InitData[], merge: MergeParams):Promise<InitData[]> => {
  let result = data
  if (merge.col) { // 设置列的合并
    result = cloneDeep(await getColSpanMethod(merge.col, data) || data)
  }
  if (merge.row) { // 设置行的合并
    result = await getRowSpanMethod(result, merge.row)
  }
  return result
}

// getMergeData(initData, {
//   row: ['id', 'name'],
//   col: ['amount1', 'amount2']
// })
